package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

/**
 * @author: lei
 * @Date 2021/11/25 20:04
 **/
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    /**
     * 2.1、客户列表分页（名称模糊查询）
     * Response Example Value：
     * {
     *     "total": 3,
     *     "rows": [
     *         {
     *             "customerId": 1,
     *             "customerName": "家乐福（青石桥店）",
     *             "contacts": "王二麻子",
     *             "phoneNumber": "13555555555",
     *             "address": "成都市锦江区大业路6号COSMO财富中心F3",
     *             "remarks": "家乐福超市稳定客户"
     *         }, ……
     *     ]
     */
    @PostMapping("/customer/list")
    public Map list(HttpServletRequest request){
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        String customerName = request.getParameter("customerName");
       Map<String,Object> map = customerService.list(page,rows,customerName);
        return map;

    }

    /**
     * 请求URL：http://localhost:8080/ customer/save?customerId=1
     * 请求参数：Customer customer
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO

     */
    @PostMapping("customer/save")
    public ServiceVO saveOrUpdate(HttpServletRequest request, @RequestParam(value = "customerId",required = false) Integer customerId){
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Customer customer = new Customer();
            BeanUtils.populate(customer,parameterMap);
            if (customerId!=null){
                customer.setCustomerId(customerId);
            }
            customerService.saveOrUpdate(customer);
            return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 2.3、客户删除（支持批量删除）
     * 请求URL：http://localhost:8080/customer/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     * Response Example Value：
     * {
     *     "code": 100,
     *     "msg": "请求成功",
     *     "info": null
     * }
     */
    @PostMapping("customer/delete")
    public ServiceVO delete(HttpServletRequest request){
        String ids = request.getParameter("ids");
        customerService.delete(ids);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS,null);
    }
}
